package app.controller.sys;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import app.model.sys.SysOrganization;
import app.service.sys.SysOrganizationService;
import base.JsonResult;
import base.tree.MenuTree;
import base.tree.Tree;

@Controller
@RequestMapping("/sys/organization")
public class SysOrganizationController {
	
	private Logger logger = Logger.getLogger(SysOrganizationController.class);
	
	@Resource
	private SysOrganizationService sysOrganizationService;
	
	@ResponseBody
	@RequestMapping("/tree.do")
	public Tree tree(){
		List<Map<String, Object>> queryMenuList = sysOrganizationService.queryList();
		Tree tree = new Tree();
		tree.buildTree(queryMenuList,"orgName","parentId");
		return tree;
	}
	
	@ResponseBody
	@RequestMapping("/save.do")
	public JsonResult save(SysOrganization sysOrganization){
		if(StringUtils.isEmpty(sysOrganization.getParentId())){
			sysOrganization.setParentId(MenuTree.root);
		}
		if(StringUtils.isEmpty(sysOrganization.getId())){
			sysOrganizationService.saveEntity(sysOrganization);
		}else{
			sysOrganizationService.updateEntity(sysOrganization);
		}
		return new JsonResult("操作成功");
	}
	
	@ResponseBody
	@RequestMapping("/view.do")
	public JsonResult view(String id){
		SysOrganization sysOrganization = sysOrganizationService.queryEntityById(id);
		return new JsonResult("操作成功",sysOrganization);
	}
	
	@ResponseBody
	@RequestMapping("/del.do")
	public JsonResult del(String ids){
		sysOrganizationService.delEntity(ids);
		return new JsonResult("操作成功");
	}
}
